<?php

namespace app\common\model;

use think\Model;
use traits\model\SoftDelete;


class TeacherAudit extends Model
{

    use SoftDelete;

    

    // 表名
    protected $name = 'teacher_audit';
    
    // 自动写入时间戳字段
    protected $autoWriteTimestamp = 'integer';

    // 定义时间戳字段名
    protected $createTime = 'createtime';
    protected $updateTime = false;
    protected $deleteTime = 'deletetime';

    // 追加属性
    protected $append = [
        'give_time_text',
        'give_lessons_text',
        'holiday_area_text',
        'unholiday_area_text',
        'teacher_exp_text',
        'photo_images_text',
        'honor_images_text',
        'tags_text'
    ];


    public function getStatusList()
    {
        return [ '1' => __('Status 1'), '2' => __('Status 2'), '3' => __('Status 3')];
    }


    public function getGiveTimeTextAttr($value, $data)
    {
        $value = $value ? $value : (isset($data['give_time']) ? $data['give_time'] : '');
        return json_decode($value, true);
    }

    public function getGiveLessonsTextAttr($value, $data){
        $value = $value ? $value : (isset($data['give_lessons']) ? $data['give_lessons'] : '');
        $give_time = json_decode($value, true);
        $return = [];
        foreach ($give_time as $k => $v) {
            if($v['grade'] && $v['subject']){
                $return[] = $v;
            }
        }
        return $return;
    }

    public function getHolidayAreaTextAttr($value, $data){
        $value = $value ? $value : (isset($data['holiday_area']) ? $data['holiday_area'] : '');
        return explode(',',$data['holiday_area']);
    }

    public function getUnholidayAreaTextAttr($value, $data){
        $value = $value ? $value : (isset($data['unholiday_area']) ? $data['unholiday_area'] : '');
        return explode(',',$data['unholiday_area']);
    }

    public function getPhotoImagesTextAttr($value, $data){
        $value = $value ? $value : (isset($data['photo_images']) ? $data['photo_images'] : '');
        if($data['photo_images']){
            return explode(',',$data['photo_images']);
        }else{
            return [];
        }
    }

    public function getHonorImagesTextAttr($value, $data){
        $value = $value ? $value : (isset($data['honor_images']) ? $data['honor_images'] : '');
        if($data['honor_images']){
            return explode(',',$data['honor_images']);
        }else{
            return [];
        }
    }

    public function getTagsTextAttr($value, $data){
        $value = $value ? $value : (isset($data['tags']) ? $data['tags'] : '');
        if($data['tags']){
            return explode(',',$data['tags']);
        }else{
            return [];
        }
    }

    public function getTeacherExpTextAttr($value, $data){
        $value = $value ? $value : (isset($data['teacher_exp']) ? $data['teacher_exp'] : '');
        return json_decode($value, true);
    }

    public function give_lessons_str($value)
    {
        $value = json_decode($value, true);
        $str = '';
        foreach ($value as $k=>$v) {
            if($v['grade'] && $v['subject']){
                if($k !== 0){
                    $str .= '<br/>';
                }
                $grade = Grade::where(['id' => $v['grade']])->value('name');
                $subject = Subject::where('id',$v['subject'])->value('name');
                $str .= $grade.$subject;
            }
        }
        return $str;
    }

    public function give_time_str($value)
    {
        $week_list = ['周一', '周二', '周三', '周四', '周五', '周六', '周日'];
        $value = json_decode($value, true);
        $str = '';
        foreach ($value as $k=>$v) {
            $has = false;
            if($v['morning']){
                $str .= $week_list[$k].'早上:'.$v['morning'];
                $has = true;
            }
            if($v['afternoon']){
                $str .= $week_list[$k].'中午:'.$v['afternoon'];
                $has = true;
            }
            if($v['night']){
                $str .= $week_list[$k].'晚上:'.$v['night'];
                $has = true;
            }
            if($has){
                $str .= '<br/>';
            }
        }
        return $str;
    }

    public function user()
    {
        return $this->belongsTo('app\admin\model\User', 'user_id', 'id', [], 'LEFT')->setEagerlyType(0);
    }



}
